﻿using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc.ModelBinding;

namespace PayService.Filter
{
    public class ValidModel : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            if (!context.ModelState.IsValid)
            {
                List<ModelError> error = context.ModelState.Values.SelectMany(x => x.Errors).ToList();

                context.Result = new JsonResult(new
                {
                    error_msg = error.Select(x => x.ErrorMessage).Aggregate((i, next) => $"{i},{ next}")
                });
            }
        }
    }
}
